﻿
Namespace PL.CRMSDK
    Public Class SalesOrder_PL
        Inherits PLBase

        'Nos paramêtre pour controler si on génère les séquences ou non...
        Public Property SequenceGeneration As Boolean = True
        Public aInvoiceNoSEQ As Boolean = False

        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal queryName As String)
            MyBase.New(queryName)
        End Sub

        ''' <summary>
        ''' Cette procedure est appellé pour chaque ligne qui sera traité.
        ''' quand le Me.MetaDataTable.Adapter.Update() est appelé via ApplyChanges()
        ''' Si on veut faire de la logique sur les lignes il faut vérifier le 
        ''' DataRowState.Added, DataRowState.Modified etc...
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Public Sub OnRowUpdating(ByVal sender As Object, ByVal e As SqlRowUpdatingEventArgs) Handles MetaDataTable.OnRowUpdating

            If e.Command.CommandText = "bsp_SalesOrder_PL_Save" Then


                'Pour le Save sur un ADD on assign la sequence
                Me.MetaDataTable.Adapter.InsertCommand.Parameters("@OrderNoSEQ").Value = SequenceGeneration
                Me.MetaDataTable.Adapter.InsertCommand.Parameters("@InvoiceNoSEQ").Value = aInvoiceNoSEQ
                '
                Me.MetaDataTable.Adapter.UpdateCommand.Parameters("@OrderNoSEQ").Value = SequenceGeneration
                Me.MetaDataTable.Adapter.UpdateCommand.Parameters("@InvoiceNoSEQ").Value = aInvoiceNoSEQ


            End If

        End Sub

        Public Overrides Function Delete() As Boolean
            Throw New NotImplementedException("Delete To Come...")
            Return False
        End Function
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="row"></param>
        ''' <remarks></remarks>
        Public Shared Sub GenerateInvoiceNumber(ByVal row As DataRow)
            Using so As SalesOrder_PL = New SalesOrder_PL("SalesOrder")
                so.MetaDataTable.GetMetaCommand()
                so.MetaDataTable.MetaCommand.SQLWhere = String.Format("SalesOrderId = '{0}'", row("SalesOrderId").ToString)
                so.InitData(False)
                'OutStantingIssue: On assigne une valeur pour que le dataset contienne un changement.
                so.Dataset.Tables(0).Rows.Find(row("SalesOrderId")).Item("InvoiceDate") = Date.Now
                so.aInvoiceNoSEQ = True
                so.SequenceGeneration = False
                so.Save()
            End Using
        End Sub

        Public Shared Sub InvoiceTimesheet(ByVal dateFrom As Date, ByVal dateTo As Date, ByVal salesOrderID As Guid, ByVal customerID As Guid)
            Using command As SqlCommand = CommonExt.Base.Connection.Command

                command.CommandText = "bsp_SetInvoicedTimesheetHour"
                command.CommandType = CommandType.StoredProcedure

                command.Parameters.AddWithValue("@FromDate", dateFrom)
                command.Parameters.AddWithValue("@ToDate", dateTo)
                command.Parameters.AddWithValue("@OrderID", salesOrderID)
                command.Parameters.AddWithValue("@customer", customerID)
                command.Parameters.AddWithValue("@StatusID", IdRepository.APP_TIMESHEETHOURSTATUS_INVOICED)

                command.ExecuteNonQuery()
            End Using

        End Sub

        Public Shared Sub UnlinkTimesheet(ByVal saleOrderID As Guid)
            Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing

            Try
                sqlcmd = MetaQuery.GetSPCmdByMetaName("TimeDetail_Unlink")

                sqlcmd.Parameters("@OrderID").Value = saleOrderID
                sqlcmd.Parameters("@StatusID").Value = IdRepository.APP_TIMESHEETHOURSTATUS_TOINVOICE

                sqlcmd.ExecuteReader(CommandBehavior.CloseConnection)

                'ReloadActiveGrid()
            Catch sqlex As System.Data.SqlClient.SqlException
                Throw sqlex
            Finally
                If sqlcmd IsNot Nothing Then
                    sqlcmd.Connection.Close()
                    sqlcmd.Dispose()
                End If
            End Try
        End Sub


        Public Overrides Function Validate() As Boolean
            'Throw New NotImplementedException("Validate To Come...")
            Return True
        End Function
    End Class
End Namespace
